<script>
import {validatePass} from "@/utils/validate";
import {mapActions} from "vuex";

export default {
  name: "Login",
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {validator: validatePass, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      updateMenuList: 'menu/updateMenuList',
      updateUserInfo: 'user/updateUserInfo',
      updateToken: 'user/updateToken'
    }),
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.updateToken();
          this.updateMenuList();
          this.updateUserInfo();
          this.$router.push('/');
          this.$message.success('登录成功');
        } else {
          this.$message.error('登录失败');
        }
      })
    },
  }
}
</script>

<template>
  <section class="loginBG">
    <el-card class="loginCard">
      <template #header>
        <div class="loginTitle">后台管理系统</div>
      </template>
      <el-form ref="form" :model="form" :rules="rules" label-width="60px" hide-required-asterisk>
        <el-form-item label="用户名" prop="username">
          <el-input id="username" v-model="form.username" clearable/>
        </el-form-item>
        <el-form-item :label="'密\xa0\xa0\xa0\xa0码'" prop="password">
          <el-input id="password" type="password" clearable show-password @keyup.enter.native="onSubmit"
                    v-model="form.password"/>
        </el-form-item>
        <el-form-item class="submit-box">
          <el-button class="submit" type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </section>
</template>

<style scoped>
.loginBG {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100vh;
  background: linear-gradient(to right bottom, #3e68ff, #8713c0);
}

.loginCard {
  width: 600px;
  margin-right: 100px;
}

.loginTitle {
  font-size: 30px;
}

::v-deep .submit-box .el-form-item__content {
  margin-left: 5px !important;
}

.submit {
  width: 100%;
}
</style>